package com.ymatou.iapi.app.keyword.testcase;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.app.keyword.parameter.CateProBean;
import com.ymatou.iapi.app.keyword.service.CateProCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P2;

/*
 * 搜索分类商品按照买手和商户的dsr和rankscore综合分排序下发结果接口
 * */
public class Ts_CatePro{
	private static CateProBean cateproBean;
	private static CateProCall cateproCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile(" 首页分类商品接口");
	}

	@Before
	public void caseUp() {
		cateproBean=new CateProBean();
		cateproCall=new CateProCall();
		
	}

	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(cateproCall.getOpurl());
		//Logger.generateResult();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_CatePro_001() {
		Logger.start(true,"首页分类商品接口- 验证接口返回公共数据");
		
		try {
			//设置参数
			cateproBean.setModule("pdt");
			cateproBean.setAppkey(1);
			cateproBean.setTimeStamp("0");
			cateproBean.setPlatform("pc");
			cateproBean.setFilter("term(fparentcategoryarrayids,1211");
			cateproBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateproBean.setLimit("0,100");
			cateproBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateproBean.setExplain("true");
			
			cateproCall.setData(cateproBean);
			cateproCall.callService();
			
			JSONArray hits = cateproCall.getHits();
			
			Logger.verifyEquals("OK",cateproCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(cateproCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, cateproCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, cateproCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyNotNull(cateproCall.getCommonInfo("hostip"), "服务器ip不为空");
			Logger.verifyNotNull(cateproCall.getCommonInfo("requestid"), "requestid不为空");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_CatePro_002() {
		Logger.start(true,"首页分类商品接口- 验证接口返回的商品是否重复");
		
		try {
			//设置参数
			cateproBean.setModule("pdt");
			cateproBean.setAppkey(1);
			cateproBean.setTimeStamp("0");
			cateproBean.setPlatform("pc");
			cateproBean.setFilter("term(fparentcategoryarrayids,1211");
			cateproBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateproBean.setLimit("0,100");
			cateproBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateproBean.setExplain("true");
			
			cateproCall.setData(cateproBean);
			cateproCall.callService();
			
			JSONArray hitsAry = cateproCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String sproductid = json.getString("sproductid");
				
				if (list.contains(sproductid) == true) {

					Logger.verifyEquals(false, list.contains(sproductid), "验证第" + i+ "条返回数据的noteid存在重复值" + sproductid);

				}else{
					Logger.verifyEquals(false, list.contains(sproductid), "验证第" + i+ "条返回数据的noteid不存在重复值" + sproductid);
					list.add(sproductid);
				}
			
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_CatePro_003() {
		Logger.start(true,"首页分类商品接口- 验证接口返回商品是否按照dsr和rankscore综合分排序");
		
		try {
			//设置参数
			cateproBean.setModule("pdt");
			cateproBean.setAppkey(1);
			cateproBean.setTimeStamp("0");
			cateproBean.setPlatform("pc");
			cateproBean.setFilter("term(fparentcategoryarrayids,1211)");
			cateproBean.setFl("sproductid,source,rankscore,dsr,sellerid");
			cateproBean.setLimit("0,10");
			cateproBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,module:firstpagecatepro");
			cateproBean.setExplain("true");
			
			cateproCall.setData(cateproBean);
			cateproCall.callService();
			
			for (int i = 1; i <=cateproCall.getHits().length(); i++) {
				for (int j = i+1; j <= cateproCall.getHits().length(); j++) {
					
					String noteInfo1 = cateproCall.getHits().get(i - 1).toString();
					JSONObject noteInfoJson1 = new JSONObject(noteInfo1);
					int rankscore1 =  noteInfoJson1.getInt("rankscore");
					int dsr1 =  noteInfoJson1.getInt("dsr");
					 //doc['rankscore'].value * 5 + doc['dsr'].value * 20 得到结果进行排序
					int scoreOne = rankscore1*5 + dsr1*20; 
					
					String noteInfo2 = cateproCall.getHits().get(j - 1).toString();
					JSONObject noteInfoJson2 = new JSONObject(noteInfo2);
					int rankscore2=  noteInfoJson2.getInt("rankscore");
					int dsr2=  noteInfoJson2.getInt("dsr");
					int scoreTwo = rankscore2*5 + dsr2*20;
					
					//Logger.verifyEquals(true, popularity1>0, "买手商品数量大于0");
					Logger.verifyEquals(true, scoreOne>=scoreTwo, "检查排在第"+i+"位的综合分值为"+scoreOne+"的商品比排在第"+j+"位的综合分值为:"+scoreTwo+"商品值高");
				}
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
